package com.changgou.service.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.service.admin.dao.AdminMapper;
import com.changgou.service.admin.service.AdminService;
import com.changgou.system.pojo.TbAdmin;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, TbAdmin> implements AdminService {

    @Override
    public void add(TbAdmin tbAdmin) {
        String gensalt = BCrypt.gensalt();
        String hashpwed = BCrypt.hashpw(tbAdmin.getPassword(), gensalt);
        tbAdmin.setPassword(hashpwed);
        save(tbAdmin);
    }

    @Override
    public boolean login(TbAdmin tbAdmin) {
        // 根据登录名在数据库中查询管理员信息
        String loginName = tbAdmin.getLoginName();
        TbAdmin tbAdmin1 = lambdaQuery()
                .eq(loginName != null, TbAdmin::getLoginName, loginName)
                .eq(TbAdmin::getStatus, "1")
                .one();

        if(ObjectUtil.isEmpty(tbAdmin1)){
            return false;
        }
        // 校验密码是否正确
        boolean checkpw = BCrypt.checkpw(tbAdmin.getPassword(),tbAdmin1.getPassword());

        if (!checkpw){
            return false;
        }
        return true;
    }
}
